//
//  WZOffMapController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/8/16.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZOffMapController.h"
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import "Masonry.h"
#import <BaiduMapAPI_Search/BMKGeocodeSearch.h>
#import <BaiduMapAPI_Search/BMKGeocodeSearchOption.h>
#import "MBProgressHUD.h"

#define LocationService_message @"您未打开定位服务!请前往:设置->隐私->定位服务中开启"

@interface WZOffMapController ()<BMKLocationServiceDelegate,BMKMapViewDelegate,BMKGeoCodeSearchDelegate>
@property (nonatomic,strong) BMKMapView *mapView;
@property (nonatomic,strong) BMKLocationService *service;
@property (nonatomic,strong) BMKGeoCodeSearch *search;
@property (nonatomic,strong) BMKUserLocation *location;
@property (nonatomic,copy) NSString *mylocation;
@end

@implementation WZOffMapController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setNewTitleCenter:@"离线地图"];
    self.mapView = [[BMKMapView alloc]init];
    self.mapView.delegate = self;
    self.mapView.zoomLevel = 18;
    [self.view addSubview:self.mapView];
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"backtobegin"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backToBegin) forControlEvents:UIControlEventTouchUpInside];
    [_mapView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(30);
    }];

}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    // 判断本应用是否允许定位
    CLAuthorizationStatus type = [CLLocationManager authorizationStatus];
    if (![CLLocationManager locationServicesEnabled] || type == kCLAuthorizationStatusDenied) {
        
        [MBProgressHUD hideHUDForView:_mapView];
        [self setupAlertView];
    }
}


-(void)mapViewDidFinishLoading:(BMKMapView *)mapView
{
    if (mapView == _mapView) {
        [self configServer];
    }
}
-(void)configServer
{
    _service = [[BMKLocationService alloc]init];
    _service.delegate = self;
    [_service startUserLocationService];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.mapView animated:YES];
    hud.labelText = @"正在定位...";
    _search = [[BMKGeoCodeSearch alloc]init];
    _search.delegate = self;
    
}
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
     [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    [_service stopUserLocationService];
    _location = userLocation;
    BMKReverseGeoCodeOption *option = [BMKReverseGeoCodeOption new];
    option.reverseGeoPoint = userLocation.location.coordinate;
    [_search reverseGeoCode:option];
}

-(void)addAnnotation:(BMKUserLocation*)userlocation
{
    BMKPointAnnotation *annotation = [BMKPointAnnotation new];
    annotation.title =  self.mylocation;
    annotation.coordinate = userlocation.location.coordinate;
    [self.mapView addAnnotation:annotation];
}
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    self.mylocation = result.address;
    [self addAnnotation:_location];
    [MBProgressHUD hideHUDForView:_mapView];
    [self addinfomationView];
    
}
-(void)addinfomationView
{
    UILabel *lable = [[UILabel alloc]init];
    lable.backgroundColor = [UIColor whiteColor];
    lable.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.numberOfLines = 0 ;
    lable.text = self.mylocation;
    [self.mapView addSubview:lable];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(80);
    }];
}
-(void)dealloc
{
    
    _mapView.delegate = nil;
    _service.delegate = nil;
    _search.delegate = nil;
    if (_mapView) {
        _mapView = nil;
    }
}
-(void)backToBegin
{
//    BMKCoordinateRegion region = BMKCoordinateRegionMakeWithDistance(_location.location.coordinate, 1000.0f, 1000.0f);
//    [self.mapView setRegion:region animated:YES];
    [_mapView setCenterCoordinate:_location.location.coordinate animated:YES];
}
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorRed;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        //设置大头针在地图上是否可以拖动
        newAnnotationView.draggable = NO;
        [newAnnotationView setSelected:YES animated:YES];
        return newAnnotationView;
    }
    return nil;
}


#pragma mark - 定位弹框

- (void)setupAlertView {
    
    if (!IsIOS8) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"开启定位" message:LocationService_message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        
    } else {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"开启定位" message:LocationService_message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:cancel];
        [alert addAction:confirm];
        [self presentViewController:alert animated:YES completion:nil];
    }
}

@end
